﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using ZX.HIS.IDAL;
using ZX.HIS.DBUtility;//Please add references
using System.Data;
using System.Collections.Generic;


namespace ZX.HIS.SQLServerDAL
{
	/// <summary>
	/// 数据访问类:DrugUsage
	/// </summary>
	public partial class DrugUsage:IDrugUsage
	{
		public DrugUsage()
		{ }
        #region Depart自写方法
        /// <summary>
        /// 分页显示药品用发表
        /// </summary>
        /// <returns></returns>
      
        public List<ZX.HIS.Model.DrugUsage> DrugUsageList(int PageIndex, int PageSize)
        {
            StringBuilder SQL = new StringBuilder();
            SQL.AppendLine("SELECT * FROM ( ");
            SQL.AppendLine(" SELECT ROW_NUMBER() OVER (");
            SQL.AppendLine("order by DrugUsageId desc");
            SQL.AppendLine(")AS aa, tbDrugUsage.DrugUsageId,tbDrugUsage.HospitalId,tbDrugUsage.DrugUsageName,tbHospital.HospitalName");
            SQL.AppendLine("  from tbDrugUsage INNER JOIN tbHospital on tbDrugUsage.HospitalId=tbHospital.HospitalId ");
            SQL.Append(" ) TT");
            SQL.AppendLine(" WHERE aa between " + ((PageIndex - 1) * PageSize + 1) + " AND " + PageIndex * PageSize);
            return SqlDBhelper.GetList<ZX.HIS.Model.DrugUsage>(SQL.ToString());
        }
        /// <summary>
        /// 显示药品用法
        /// </summary>
        /// <returns></returns>
        public List<ZX.HIS.Model.DrugUsage> DrugUsageShow()
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("SELECT  tbDrugUsage.DrugUsageId,tbDrugUsage.HospitalId,tbDrugUsage.DrugUsageName,tbHospital.HospitalName  from tbDrugUsage INNER JOIN tbHospital on tbDrugUsage.HospitalId=tbHospital.HospitalId ");
            return SqlDBhelper.GetList<ZX.HIS.Model.DrugUsage>(sql.ToString());
        }
        /// <summary>
        /// 总条数查询
        /// </summary>
        /// <param name="StrWhere"></param>
        /// <returns></returns>
        public int DrugUsageCount()
        {
            StringBuilder SQL = new StringBuilder();
            SQL.AppendLine("SELECT COUNT(*) FROM tbDrugUsage");

            return SqlDBhelper.ExecuteScalarInt(SQL.ToString());
        }
        /// <summary>
        /// 添加药品用发表
        /// </summary>
        /// <param name="DrugUsageModel"></param>
        /// <returns></returns>
        public int AddDrugUsage(ZX.HIS.Model.DrugUsage DrugUsageModel)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO tbDrugUser(");
            strSql.Append("HospitalId,DrugUsageName)");
            strSql.Append("values (");
            strSql.Append("@HospitalId,@DrugUsageName)");
            strSql.Append(";SELECT @@IDENTITY");
            SqlParameter[] parameter = { new SqlParameter("@HospitalId", SqlDbType.Int, 4), new SqlParameter("@DrugUsageName", SqlDbType.VarChar, 100) };
            parameter[0].Value = DrugUsageModel.HospitalId;
            parameter[1].Value = DrugUsageModel.DrugUsageName;
            return SqlDBhelper.ExecuteNonQuery(strSql.ToString(), parameter);

        }
        /// <summary>
        /// 修改药品信息表
        /// </summary>
        /// <param name="DrugUsageModel"></param>
        /// <returns></returns>
        public int UpdateDrugUsage(ZX.HIS.Model.DrugUsage DrugUsageModel)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("UPDATE tbDrugUsage SET ");
            strSql.Append("HospitalId=@HospitalId,");
            strSql.Append("DrugUsageName=@DrugUsageName");
            strSql.Append(" WHERE DrugUsageId=@DrugUsageId");
            SqlParameter[] parameters = {
                    new SqlParameter("@HospitalId", SqlDbType.Int,4),
                    new SqlParameter("@DrugUsageName", SqlDbType.VarChar,100),
                    new SqlParameter("@DrugUsageId", SqlDbType.Int,4)};
            parameters[0].Value = DrugUsageModel.HospitalId;
            parameters[1].Value = DrugUsageModel.DrugUsageName;
            parameters[2].Value = DrugUsageModel.DrugUsageId;
            return SqlDBhelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 删除药品信息表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteDrugUsage(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE DROM tbDrugUsage");
            strSql.Append(" WHERE DrugUsageId=@DrugUsageId");
            SqlParameter[] parameters = {
                     new SqlParameter("@DrugUsageId", SqlDbType.Int,4)
             };
            parameters[0].Value = id;

            return SqlDBhelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        #endregion
        #region  BasicMethod

        ///// <summary>
        ///// 得到最大ID
        ///// </summary>
        //public int GetMaxId()
        //{
        //return DbHelperSQL.GetMaxID("DrugUsageId", "tbDrugUsage"); 
        //}

        ///// <summary>
        ///// 是否存在该记录
        ///// </summary>
        //public bool Exists(int DrugUsageId)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select count(1) from tbDrugUsage");
        //    strSql.Append(" where DrugUsageId=@DrugUsageId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@DrugUsageId", SqlDbType.Int,4)
        //    };
        //    parameters[0].Value = DrugUsageId;

        //    return DbHelperSQL.Exists(strSql.ToString(),parameters);
        //}
      
      
        
        ///// <summary>
        ///// 增加一条数据
        ///// </summary>
        //public int Add(ZX.HIS.Model.DrugUsage model)
        //{
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("insert into tbDrugUsage(");
        //    strSql.Append("HospitalId,DrugUsageName)");
        //    strSql.Append(" values (");
        //    strSql.Append("@HospitalId,@DrugUsageName)");
        //    strSql.Append(";select @@IDENTITY");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@HospitalId", SqlDbType.Int,4),
        //            new SqlParameter("@DrugUsageName", SqlDbType.VarChar,100)};
        //    parameters[0].Value = model.HospitalId;
        //    parameters[1].Value = model.DrugUsageName;

        //    object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
        //    if (obj == null)
        //    {
        //        return 0;
        //    }
        //    else
        //    {
        //        return Convert.ToInt32(obj);
        //    }
        //}
       
        ///// <summary>
        ///// 更新一条数据
        ///// </summary>
        //public bool Update(ZX.HIS.Model.DrugUsage model)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("update tbDrugUsage set ");
        //    strSql.Append("HospitalId=@HospitalId,");
        //    strSql.Append("DrugUsageName=@DrugUsageName");
        //    strSql.Append(" where DrugUsageId=@DrugUsageId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@HospitalId", SqlDbType.Int,4),
        //            new SqlParameter("@DrugUsageName", SqlDbType.VarChar,100),
        //            new SqlParameter("@DrugUsageId", SqlDbType.Int,4)};
        //    parameters[0].Value = model.HospitalId;
        //    parameters[1].Value = model.DrugUsageName;
        //    parameters[2].Value = model.DrugUsageId;

        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}

       
        ///// <summary>
        ///// 删除一条数据
        ///// </summary>
        //public bool Delete(int DrugUsageId)
        //{
			
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("delete from tbDrugUsage ");
        //    strSql.Append(" where DrugUsageId=@DrugUsageId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@DrugUsageId", SqlDbType.Int,4)
        //    };
        //    parameters[0].Value = DrugUsageId;

        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}
       
        ///// <summary>
        ///// 批量删除数据
        ///// </summary>
        //public bool DeleteList(string DrugUsageIdlist )
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("delete from tbDrugUsage ");
        //    strSql.Append(" where DrugUsageId in ("+DrugUsageIdlist + ")  ");
        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}


        ///// <summary>
        ///// 得到一个对象实体
        ///// </summary>
        //public ZX.HIS.Model.DrugUsage GetModel(int DrugUsageId)
        //{
			
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select  top 1 DrugUsageId,HospitalId,DrugUsageName from tbDrugUsage ");
        //    strSql.Append(" where DrugUsageId=@DrugUsageId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@DrugUsageId", SqlDbType.Int,4)
        //    };
        //    parameters[0].Value = DrugUsageId;

        //    ZX.HIS.Model.DrugUsage model=new ZX.HIS.Model.DrugUsage();
        //    DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
        //    if(ds.Tables[0].Rows.Count>0)
        //    {
        //        return DataRowToModel(ds.Tables[0].Rows[0]);
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //}


        ///// <summary>
        ///// 得到一个对象实体
        ///// </summary>
        //public ZX.HIS.Model.DrugUsage DataRowToModel(DataRow row)
        //{
        //    ZX.HIS.Model.DrugUsage model=new ZX.HIS.Model.DrugUsage();
        //    if (row != null)
        //    {
        //        if(row["DrugUsageId"]!=null && row["DrugUsageId"].ToString()!="")
        //        {
        //            model.DrugUsageId=int.Parse(row["DrugUsageId"].ToString());
        //        }
        //        if(row["HospitalId"]!=null && row["HospitalId"].ToString()!="")
        //        {
        //            model.HospitalId=int.Parse(row["HospitalId"].ToString());
        //        }
        //        if(row["DrugUsageName"]!=null)
        //        {
        //            model.DrugUsageName=row["DrugUsageName"].ToString();
        //        }
        //    }
        //    return model;
        //}

        ///// <summary>
        ///// 获得数据列表
        ///// </summary>
        //public DataSet GetList(string strWhere)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select DrugUsageId,HospitalId,DrugUsageName ");
        //    strSql.Append(" FROM tbDrugUsage ");
        //    if(strWhere.Trim()!="")
        //    {
        //        strSql.Append(" where "+strWhere);
        //    }
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        ///// <summary>
        ///// 获得前几行数据
        ///// </summary>
        //public DataSet GetList(int Top,string strWhere,string filedOrder)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select ");
        //    if(Top>0)
        //    {
        //        strSql.Append(" top "+Top.ToString());
        //    }
        //    strSql.Append(" DrugUsageId,HospitalId,DrugUsageName ");
        //    strSql.Append(" FROM tbDrugUsage ");
        //    if(strWhere.Trim()!="")
        //    {
        //        strSql.Append(" where "+strWhere);
        //    }
        //    strSql.Append(" order by " + filedOrder);
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        ///// <summary>
        ///// 获取记录总数
        ///// </summary>
        //public int GetRecordCount(string strWhere)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select count(1) FROM tbDrugUsage ");
        //    if(strWhere.Trim()!="")
        //    {
        //        strSql.Append(" where "+strWhere);
        //    }
        //    object obj = DbHelperSQL.GetSingle(strSql.ToString());
        //    if (obj == null)
        //    {
        //        return 0;
        //    }
        //    else
        //    {
        //        return Convert.ToInt32(obj);
        //    }
        //}
        ///// <summary>
        ///// 分页获取数据列表
        ///// </summary>
        //public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("SELECT * FROM ( ");
        //    strSql.Append(" SELECT ROW_NUMBER() OVER (");
        //    if (!string.IsNullOrEmpty(orderby.Trim()))
        //    {
        //        strSql.Append("order by T." + orderby );
        //    }
        //    else
        //    {
        //        strSql.Append("order by T.DrugUsageId desc");
        //    }
        //    strSql.Append(")AS Row, T.*  from tbDrugUsage T ");
        //    if (!string.IsNullOrEmpty(strWhere.Trim()))
        //    {
        //        strSql.Append(" WHERE " + strWhere);
        //    }
        //    strSql.Append(" ) TT");
        //    strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        ///*
        ///// <summary>
        ///// 分页获取数据列表
        ///// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@tblName", SqlDbType.VarChar, 255),
        //            new SqlParameter("@fldName", SqlDbType.VarChar, 255),
        //            new SqlParameter("@PageSize", SqlDbType.Int),
        //            new SqlParameter("@PageIndex", SqlDbType.Int),
        //            new SqlParameter("@IsReCount", SqlDbType.Bit),
        //            new SqlParameter("@OrderType", SqlDbType.Bit),
        //            new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
        //            };
        //    parameters[0].Value = "tbDrugUsage";
        //    parameters[1].Value = "DrugUsageId";
        //    parameters[2].Value = PageSize;
        //    parameters[3].Value = PageIndex;
        //    parameters[4].Value = 0;
        //    parameters[5].Value = 0;
        //    parameters[6].Value = strWhere;	
        //    return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        //}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

